<?php
    header('Access-Control-Allow-Origin: *');
	header("Access-Control-Allow-Method:POST,GET");
    header ( "Content-Type: text/html; charset=UTF-8" );
    require_once 'mysql.function.php';
	require_once 'response.class.php';
    //获取POST参数  前台需要初始化page为0
    
    $rawPostData = file_get_contents("php://input");
	$postData = json_decode($rawPostData,true);

    if(empty($postData["page"])){
    	$start = 0;
    } else{
    	$page = $postData["page"];
    	$start = $page * 10;
    }
    
    //实例化
    $sqlhelper = new sqlhelper();
	$re = new Response();

	$sql = "select id,nickname,content,createtime from TreeHole where state = 1 order by id desc limit $start,10";

    if ($sqlhelper->getResultNum($sql) == 0) {
		$message = array(
			'content' => "NULL",
		);
		$re->show(200,'fail',$message);
		exit();
	}

	$arr = $sqlhelper->fetchall($sql);
    
    
    for ($i=0; $i <10; $i++) {
        $commnetId = $arr["$i"]["id"];//获取当前树洞id
    	$sql1 = "select id,treecomment from TreeComment where id = '$commnetId' ";//查找id对应的评论内容
    	$num = $sqlhelper->getResultNum($sql1);//获取评论数量
    	$arr["$i"]["comnum"] = $num;//把评论数量加进去

    	if($num == 0){       
    		$comarray = array(
    			'treecomment' => NULL,
    			);
    		$arr["$i"]["treecomment"] = $comarray;
    	}else{
    		$comarr = $sqlhelper->fetchall($sql1);//获取评论内容
    	
           $n = array();
        for ($j=0; $j < $num ; $j++) { 
    		$n["$j"] = $comarr["$j"]["treecomment"];
    	}
        
    	$arr["$i"]["treecomment"] = $n;
    	}
    	
    	//var_dump($comarray);
    }
        
	//返回json数据
	//增加属性
     for ($x=0; $x<10; $x++) {
         $arr["$x"]["success"] = false; 
      } 
    
	$message = array(
		'message' => $arr,
	);
     $sqlhelper->close_connect();
     //var_dump($comarr);
     //var_dump($arr);
	$re->show(200,'success',$message);
	exit();
?>